package com.raining.inningz.ui.play;

import java.util.List;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.raining.inningz.R;
import com.raining.inningz.adapter.MatchListAdapter;
import com.raining.inningz.baseclass.BaseFragment;
import com.raining.inningz.constants.IFragmentNameConstants;
import com.raining.inningz.constants.IParamConstants;
import com.raining.inningz.database.dao.Matches;

public class PlayFragment  extends BaseFragment implements OnClickListener{
	
	
	private LayoutInflater mInflater;
	private View view;
	
	
	
	private RelativeLayout relativeLayoutStart;
	private RelativeLayout relativeLayoutReceive;
	
	
	private ListView listViewPreRecords;
	
	
	private PlayMatchInfoFragment playFragment;
	
	private List<Matches> matchesList;
	
	private MatchListAdapter matchListAdapter;
	
	private RelativeLayout  relLayout1;
	
	private TextView txt1;
	
	private PlayScorecardFragment playScorecardFragment;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		
		view = inflater.inflate(R.layout.fragment_play, container, false);
		context = mInflater.getContext();

		initialiseUI();
		
		setData();
		
		setListner();

		return view;
	}

	

	



	private void initialiseUI() {
		
		setHeaderIntialize(view);
		setHeaderTitle(resources.getString(R.string.inningz),Color.BLACK);
		
		relativeLayoutStart=(RelativeLayout)view.findViewById(R.id.relativeLayoutStart);
		relativeLayoutReceive=(RelativeLayout)view.findViewById(R.id.relativeLayoutReceive);
		listViewPreRecords=(ListView)view.findViewById(R.id.listViewPreRecords);
		
		relLayout1=(RelativeLayout)view.findViewById(R.id.relLayout1);
		txt1=(TextView)view.findViewById(R.id.txt1);
		
	}
	
	private void setData() {
	
		matchesList=matchesDao.queryBuilder().list();
		if(matchesList.size()>0){
			relLayout1.setVisibility(View.VISIBLE);
		}
		else{
			relLayout1.setVisibility(View.GONE);
		}
		
		txt1.setText(String.format(resources.getString(R.string.title1), matchesList.size()));
		matchListAdapter=new MatchListAdapter(context, matchesList);
		listViewPreRecords.setAdapter(matchListAdapter);
		
		
	}
	private void setListner() {
		
		relativeLayoutStart.setOnClickListener(this);
		relativeLayoutReceive.setOnClickListener(this);
		listViewPreRecords.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
			
				
				playScorecardFragment=new PlayScorecardFragment();
				changeFragment(playScorecardFragment, IFragmentNameConstants.PLAY_SCORE_CARD);
				
			}
		});
		
	}



	@Override
	public void onClick(View v) {
		
		if(v==relativeLayoutStart){
			playFragment=new PlayMatchInfoFragment();
			changeFragment(playFragment,IFragmentNameConstants.PLAY_MATCH_INFO);
		}
		else if(v==relativeLayoutReceive){
			
		}
	}

}
